package util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
	 
	public class SQLiteHelper extends SQLiteOpenHelper {

	       private final static String DATABASE_NAME = "aibilian.db";
	       private final static int DATABASE_VERSION = 1;
	       private final static String TABLE_NAME = "work";
	       private Context context ;

	       public SQLiteHelper(Context context) {
	    	   
	              super(context, DATABASE_NAME, null, DATABASE_VERSION);
	              this.context=context;
	       }
	 

	       public void onCreate(SQLiteDatabase db) {

	            String sql = "CREATE TABLE " + TABLE_NAME
	            		  + "(_id INTEGER PRIMARY KEY," 
	            		  + " type varchar(50),value varchar(200),time varchar(20))";
	            System.out.println(sql);
	              db.execSQL(sql);
	       }
	 
	 
	       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	              String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
	              db.execSQL(sql);
	              onCreate(db);
	       }
	 

	       //��ȡ�α�
	       public Cursor selectsql(String sql) {
	              SQLiteDatabase db = this.getReadableDatabase();
	              Cursor cursor =db.rawQuery(sql, null);  
	              return cursor;
	       }
	       //����һ����¼
	       public long insert(String type,String value,String time) {
	              SQLiteDatabase db = this.getWritableDatabase();
	              ContentValues cv = new ContentValues();
	              cv.put("type", type);
	              cv.put("value", value);
	              cv.put("time", time);
	              long row = db.insert(TABLE_NAME, null, cv);
	              return row;
	       }
	       
	       //����������ѯ
	       public Cursor query(String[] args) {
	           SQLiteDatabase db = this.getReadableDatabase();
	           Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE BookName LIKE ?", args);
	           return cursor;
	       }
	 
	       //ɾ����¼
	       public void delete(int id) {
	              SQLiteDatabase db = this.getWritableDatabase();
	              String where ="_id = ?";
	              String[] whereValue = { Integer.toString(id) };
	              db.delete(TABLE_NAME, where, whereValue);
	       }
		public void deleteAll() {
			SQLiteDatabase db = this.getWritableDatabase();
			String where =" 1=1 ";
			String[] whereValue = { };
			db.delete(TABLE_NAME, where, whereValue);
		}
	       //���¼�¼
	       public void update(String where, String state) {
	              SQLiteDatabase db = this.getWritableDatabase();
	              ContentValues cv = new ContentValues();
	              cv.put("state", state);
	              db.update(TABLE_NAME, cv, where, null);
	            
	       }
	       public boolean deleteDatabase() {
	    	   return context.deleteDatabase(DATABASE_NAME);
	    }


}
